/**
 * 
 */
package de.cbf.cam.dsa.facet.talent.helper.editing_support;

import static de.cbf.util.ui.test_support.ConstantsSWTBOTUtil.DEFAULT_SWT_BOT_KEY;

import java.text.NumberFormat;
import java.text.ParseException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;

import de.cbf.cam.dsa.facet.talent.helper.CompensationTalentReference;
import de.cbf.cam.dsa.facet.talent.test_support.ConstantsSWTBOTTalent;

/**
 * @author Nebelritter
 *
 */
public class CompensationValueEditingSupport extends EditingSupport {
	private static final Log logger = LogFactory.getLog(CompensationValueEditingSupport.class);
	
	protected NumberFormat integerFormat;

	protected TableViewer tableViewer;
	/**
	 * @param viewer
	 */
	public CompensationValueEditingSupport(ColumnViewer viewer) {
		super(viewer);		
		integerFormat = NumberFormat.getIntegerInstance();
		if (viewer instanceof TableViewer) {
			tableViewer = (TableViewer)viewer;			
		}else{
			String err_msg = "entity of wrong type in CompensationValueEditingSupport() : \n"+
					"Expected: "+TableViewer.class.getSimpleName()+"\n"+ 
					"Got: "+ (viewer == null ? "null" : viewer.getClass().getSimpleName());
			logger.warn(err_msg);
		}		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#getCellEditor(java.lang.Object)
	 */
	@Override
	protected CellEditor getCellEditor(Object element) {	
		TextCellEditor cellEditor = new TextCellEditor(tableViewer.getTable());
		cellEditor.getControl().setData(DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTTalent.FACET_Talent_COMPOSITE_COMPENSATION_TALENTS_TABLE_TEXT_VALUE_ID);
		return cellEditor; 
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#canEdit(java.lang.Object)
	 */
	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#getValue(java.lang.Object)
	 */
	@Override
	protected Object getValue(Object element) {
		if (element instanceof CompensationTalentReference) {
			CompensationTalentReference compensationTalentRef = (CompensationTalentReference)element;
			Integer value = compensationTalentRef.getCompensationValue();
			String valueString = integerFormat.format(value);
			return valueString;
		}else{
			String err_msg = "entity of wrong type in getValue : \n"+
					"Expected: "+CompensationTalentReference.class.getSimpleName()+"\n"+ 
					"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
			logger.error(err_msg);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.EditingSupport#setValue(java.lang.Object, java.lang.Object)
	 */
	@Override
	protected void setValue(Object element, Object value) {
		if (element instanceof CompensationTalentReference && value instanceof String) {
			CompensationTalentReference compensationTalentRef = (CompensationTalentReference)element;
			String compensationValueString = (String)value;
			Number number;
			try {
				number = integerFormat.parse(compensationValueString);
				if (number instanceof Long) {
					Long numberLong = (Long)number;
					compensationTalentRef.setCompensationValue(numberLong.intValue());	
					tableViewer.update(element, null);					
				}else{
					String err_msg = "entity of wrong type in setValue : \n"+
							"Expected: "+Long.class.getSimpleName()+"\n"+ 
							"Got: "+ (number == null ? "null" : number.getClass().getSimpleName());
					logger.error(err_msg);
				}
			} catch (ParseException e) {
				logger.error(e);
			}			
		}else{
			String err_msg = "entity of wrong type in setValue : \n"+
					"Expected: "+CompensationTalentReference.class.getSimpleName()+"\n"+ 
					"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
			logger.error(err_msg);
		};
	}

}
